﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using KidsGest.Models;
using System.Data.SqlClient;
using System.Configuration;
using WorkingCode.CodeProject.PwdGen;
using System.Data;

namespace KidsGest.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        public KidsGestEntities db = new KidsGestEntities();
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListarPreInscricoes() 
        {
            var preRegistos = db.PreRegistos.Where(preregisto => preregisto.Aceite == false);

            return View(preRegistos.ToList());
        }

        //GET: /Admin/DetalhesPreCandidatura/5

        public ActionResult DetalhesPreCandidatura(int id)
        {
            PreRegistoModel preregistomodel = db.PreRegistos.Find(id);
            return View(preregistomodel);
        }

        [HttpPost]
        public ActionResult DetalhesPreCandidatura(int id)
        {
            Console.WriteLine("deves");
            if (ModelState.IsValid)
            {
                Console.WriteLine("Cheguei aqui");
                PasswordGenerator password = new PasswordGenerator();
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(modelo.Email, password.Generate(), modelo.Email, null, null, true, null, out createStatus);
                //var candidatura = db.PreRegistos.Where(preregisto => preregisto.Email.ToLower() == modelo.Email.ToLower());
                //candidatura.First().Aceite = true;
                //var cand = candidatura.First();
                //modelo.Aceite = true;
                PreRegistoModel crianca = db.PreRegistos.Find(modelo.AlunoID);
                crianca.Aceite = true;
                db.Entry(crianca).State = EntityState.Modified;
                db.SaveChanges();                

                if (createStatus == MembershipCreateStatus.Success)
                {
                    Console.WriteLine("Cheguei");
                    return RedirectToAction("PreCandidaturaSuccess", "Admin");
                }
                else
                {
                    ModelState.AddModelError("", AccountController.ErrorCodeToString(createStatus));
                }
            }
            return View();
        }

        public ActionResult PreCandidaturaSuccess() 
        {
            return View();
        }

    }
}
